package com.tangsm.spring.boot.statemachine;

import com.tangsm.spring.boot.statemachine.enums.Events;
import com.tangsm.spring.boot.statemachine.enums.States;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.statemachine.StateMachine;
import reactor.core.publisher.Mono;

/**
 * spring statemachine 有限状态机依赖
 *
 * <p>注：还有很多地方没搞清楚，暂时先到这，后续再研究</p>
 *
 * @author tangsm
 * @see <a href="https://docs.spring.io/spring-statemachine/docs/3.2.x/reference/">Spring Statemachine 官方文档</a>
 */
@SpringBootApplication
public class StatemachineApplication implements CommandLineRunner {
    @Autowired
    private StateMachine<States, Events> stateMachine;

    public static void main(String[] args) {
        SpringApplication.run(StatemachineApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {

        Mono.defer(() -> Mono.justOrEmpty(stateMachine.getState().getId()));
        stateMachine.start();
        stateMachine.sendEvent(Events.E1);
        stateMachine.sendEvent(Events.E2);
    }
}
